﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================

using System.Collections.Generic;
using XCI.Extensions;
using XCI.Helper;

namespace XCI.Core
{
    /// <summary>
    /// 通用字典
    /// </summary>
    public class Map : Dictionary<string, object>
    {
        public static Map New => new Map();

        /// <summary>
        /// 添加键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public new Map Add(string key, object value)
        {
            this[key] = value;
            return this;
        }

        /// <summary>
        /// 添加对象(属性名为键,属性值为值)
        /// </summary>
        /// <param name="obj">对象</param>
        public Map AddObject(object obj)
        {
            var data = ObjectHelper.GetPropertyKeyValuePair(obj);
            foreach (KeyValuePair<string, object> item in data)
            {
                this[item.Key] = item.Value;
            }
            return this;
        }

        /// <summary>
        /// 如果值不为空字符串,则添加键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public Map IsNotEmpty(string key, object value)
        {
            if (value != null && !string.IsNullOrEmpty(value.ToString()))
            {
                this[key] = value.ToString();
            }
            return this;
        }

        /// <summary>
        /// 如果值不为空对象,则添加键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public Map IfNotNull(string key, object value)
        {
            if (value != null)
            {
                this[key] = value.ToString();
            }
            return this;
        }

        /// <summary>
        /// 如果条件为真,则添加键值对
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public Map If(bool condition, string key, object value)
        {
            if (condition)
            {
                this[key] = value;
            }
            return this;
        }

        /// <summary>
        /// 合并两个Map集合
        /// </summary>
        /// <param name="map">待合并的Map集合</param>
        public Map Merge(Map map)
        {
            foreach (KeyValuePair<string,object> item in map)
            {
                this[item.Key] = item.Value;
            }
            return this;
        }
    }

    /// <summary>
    /// 字符串字典
    /// </summary>
    public class SMap : Dictionary<string, string>
    {
        public static SMap New => new SMap();

        /// <summary>
        /// 添加键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public new SMap Add(string key, string value)
        {
            this[key] = value;
            return this;
        }

        /// <summary>
        /// 添加对象(属性名为键,属性值为值)
        /// </summary>
        /// <param name="obj">对象</param>
        public SMap AddObject(string obj)
        {
            var data = ObjectHelper.GetPropertyKeyValuePair(obj);
            foreach (KeyValuePair<string, object> item in data)
            {
                this[item.Key] = item.Value.ToStringOrEmpty();
            }
            return this;
        }

        /// <summary>
        /// 如果值不为空字符串,则添加键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public SMap IsNotEmpty(string key, string value)
        {
            if (value.IsNotEmpty())
            {
                this[key] = value;
            }
            return this;
        }

        /// <summary>
        /// 如果条件为真,则添加键值对
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public SMap If(bool condition, string key, string value)
        {
            if (condition)
            {
                this[key] = value;
            }
            return this;
        }

        /// <summary>
        /// 合并两个Map集合
        /// </summary>
        /// <param name="map">待合并的Map集合</param>
        public SMap Merge(SMap map)
        {
            foreach (KeyValuePair<string, string> item in map)
            {
                this[item.Key] = item.Value;
            }
            return this;
        }
    }
}